using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class LIST_GADGETDataAccess : Connection
    {
        #region Local Variable
        private string _strSPInsertName = "dbo.[procLIST_GADGET_add]";
        private string _strSPUpdateName = "dbo.[procLIST_GADGET_update]";
        private string _strSPDeleteName = "dbo.[procLIST_GADGET_delete]";
        private string _strSPGetName = "dbo.[procLIST_GADGET_get]";
        private string _strSPGetAllName = "dbo.[procLIST_GADGET_getall]";
        private string _strSPGetPages = "dbo.[procLIST_GADGET_getpaged]";
        private string _strSPIsExist = "dbo.[procLIST_GADGET_isexist]";
        private string _strTableName = "[LIST_GADGET]";
        private string _strSPGetTransferOutName = "dbo.[procLIST_GADGET_gettransferout]";
        private string _strSPGetName_ByDefault = "dbo.[procLIST_GADGET_getByDefault]";
        private string _strSPGetNameCount = "dbo.[procLIST_GADGET_getcount]";
        #endregion Local Variable

        #region Method
        public LIST_GADGETInfo Get(
        String DTB,
        Int32 ID, String owner,
        ref string sErr)
        {
            LIST_GADGETInfo objEntr = new LIST_GADGETInfo();
            InitConnect();
            InitSPCommand(_strSPGetName);
            AddParameter(LIST_GADGETInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_GADGETInfo.Field.ID.ToString(), ID);
            AddParameter(LIST_GADGETInfo.Field.Owner.ToString(), owner);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}

            if (list.Rows.Count > 0)
                objEntr = new LIST_GADGETInfo(list.Rows[0]);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        public DataTable GetAll(
        String DTB, String owner,
        ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);
            AddParameter(LIST_GADGETInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_GADGETInfo.Field.Owner.ToString(), owner);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetByPos(
        String DTB,
        int pos, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);
            AddParameter("INX", pos);
            AddParameter(LIST_GADGETInfo.Field.DTB.ToString(), DTB);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public int GetCountRecord(
        String DTB,
        ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitSPCommand(_strSPGetNameCount);
            AddParameter(LIST_GADGETInfo.Field.DTB.ToString(), DTB);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }
        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(LIST_GADGETInfo objEntr, ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitSPCommand(_strSPInsertName);
            AddParameter(LIST_GADGETInfo.Field.DTB.ToString(), objEntr.DTB);
            AddParameter(LIST_GADGETInfo.Field.ID.ToString(), objEntr.ID);
            AddParameter(LIST_GADGETInfo.Field.Description.ToString(), objEntr.Description);
            AddParameter(LIST_GADGETInfo.Field.QDCode.ToString(), objEntr.QDCode);
            AddParameter(LIST_GADGETInfo.Field.ReportTmp.ToString(), objEntr.ReportTmp);
            AddParameter(LIST_GADGETInfo.Field.AutoUpdate.ToString(), objEntr.AutoUpdate);
            AddParameter(LIST_GADGETInfo.Field.IsScroll.ToString(), objEntr.IsScroll);
            AddParameter(LIST_GADGETInfo.Field.Action.ToString(), objEntr.Action);
            AddParameter(LIST_GADGETInfo.Field.Argument.ToString(), objEntr.Argument);
            AddParameter(LIST_GADGETInfo.Field.Image.ToString(), objEntr.Image);
            AddParameter(LIST_GADGETInfo.Field.Param.ToString(), objEntr.Param);
            AddParameter(LIST_GADGETInfo.Field.IsDefault.ToString(), objEntr.IsDefault);
            AddParameter(LIST_GADGETInfo.Field.ReportTmp_Print.ToString(), objEntr.ReportTmp_Print);
            AddParameter(LIST_GADGETInfo.Field.Owner.ToString(), objEntr.Owner);
            AddParameter(LIST_GADGETInfo.Field.Shared.ToString(), objEntr.Shared);
            AddParameter(LIST_GADGETInfo.Field.DataGroup.ToString(), objEntr.DataGroup);
            AddParameter(LIST_GADGETInfo.Field.Type.ToString(), objEntr.Type);
            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(LIST_GADGETInfo objEntr)
        {
            openConnection();
            InitSPCommand(_strSPUpdateName);
            AddParameter(LIST_GADGETInfo.Field.DTB.ToString(), objEntr.DTB);
            AddParameter(LIST_GADGETInfo.Field.ID.ToString(), objEntr.ID);
            AddParameter(LIST_GADGETInfo.Field.Description.ToString(), objEntr.Description);
            AddParameter(LIST_GADGETInfo.Field.QDCode.ToString(), objEntr.QDCode);
            AddParameter(LIST_GADGETInfo.Field.ReportTmp.ToString(), objEntr.ReportTmp);
            AddParameter(LIST_GADGETInfo.Field.AutoUpdate.ToString(), objEntr.AutoUpdate);
            AddParameter(LIST_GADGETInfo.Field.IsScroll.ToString(), objEntr.IsScroll);
            AddParameter(LIST_GADGETInfo.Field.Action.ToString(), objEntr.Action);
            AddParameter(LIST_GADGETInfo.Field.Argument.ToString(), objEntr.Argument);
            AddParameter(LIST_GADGETInfo.Field.Image.ToString(), objEntr.Image);
            AddParameter(LIST_GADGETInfo.Field.Param.ToString(), objEntr.Param);
            AddParameter(LIST_GADGETInfo.Field.IsDefault.ToString(), objEntr.IsDefault);
            AddParameter(LIST_GADGETInfo.Field.ReportTmp_Print.ToString(), objEntr.ReportTmp_Print);
            AddParameter(LIST_GADGETInfo.Field.Owner.ToString(), objEntr.Owner);
            AddParameter(LIST_GADGETInfo.Field.Shared.ToString(), objEntr.Shared);
            AddParameter(LIST_GADGETInfo.Field.DataGroup.ToString(), objEntr.DataGroup);
            AddParameter(LIST_GADGETInfo.Field.Type.ToString(), objEntr.Type);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        String DTB,
        Int32 ID
        )
        {
            openConnection();
            InitSPCommand(_strSPDeleteName);
            AddParameter(LIST_GADGETInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_GADGETInfo.Field.ID.ToString(), ID);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

      

        public Boolean IsExist(
        String DTB,
        Int32 ID
        )
        {
            InitConnect();
            InitSPCommand(_strSPIsExist);
            AddParameter(LIST_GADGETInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_GADGETInfo.Field.ID.ToString(), ID);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }

        private string CreateWhereClause(LIST_GADGETInfo obj)
        {
            String result = "";

            return result;
        }

        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            openConnection();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetTransferOutName);
            AddParameter("DB", dtb);
            AddParameter("FROM", from);
            AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
        #endregion Method
        public DataTable Get_ByDefault(
         String DTB,
         String User,
            String Type,
         ref string sErr)
        {
            LIST_GADGETInfo objEntr = new LIST_GADGETInfo();
            InitConnect();
            InitSPCommand(_strSPGetName_ByDefault);
            AddParameter(LIST_GADGETInfo.Field.DTB.ToString(), DTB);
            AddParameter(LIST_GADGETInfo.Field.Owner.ToString(), User);
            AddParameter(LIST_GADGETInfo.Field.Type.ToString(), Type);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}

            if (list.Rows.Count > 0)
                objEntr = (LIST_GADGETInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
    }
}
